﻿using BaseService;
using Blog.Model;
using Blog.Utility.ApiResult;
using IBaseService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Xml.Linq;
using Blog.Utility._MD5;
using AutoMapper;
using Blog.Dto.Author;
using Microsoft.AspNetCore.Authorization;

namespace Blog.Controllers
{
	[Route("api/[controller]")]
	[ApiController]
	public class AuthorController : ControllerBase
	{
		private readonly IAuthorService _authorService;

		public AuthorController(IAuthorService authorService)
		{
			_authorService = authorService;
		}

		[HttpGet("AuthorList")]
		[Authorize]
		public async Task<IActionResult> QueryList()
		{
			var list = await _authorService.QueryAsync();
			return Ok(list);
		}

		/// <summary>
		/// 用户注册
		/// </summary>
		/// <param name="userName">用户名</param>
		/// <param name="password">密码</param>
		/// <param name="name">真实姓名</param>
		/// <returns></returns>
		[HttpPost("CreateAuthor")]
		public async Task<ActionResult<ApiResult>> CreateAuthor(string userName,string password,string name)
		{
			

			List<Author> list =  await _authorService.QueryAsync();


			Author user = list.FirstOrDefault(x => x.UserName == userName);
			if (user != null) return ApiResultTool.Fail("该用户已添加");

			Author author = new Author()
			{
				UserName = userName,
				Password = _MD5.MD5Encrypt32(password),
				Name = name
			};
			
			bool res = await _authorService.CreateAsync(author);
			if (!res) return ApiResultTool.Fail("新增用户失败");
			return ApiResultTool.Success(author);
		}


		/// <summary>
		/// 删除用户
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost("DeleteAuthor")]
		[Authorize]
		public async Task<ActionResult<ApiResult>> DeleteAuthor(int id)
		{
			bool res = await _authorService.DeleteByIdAsync(id);
			if (!res) return ApiResultTool.Fail("删除用户失败");
			return ApiResultTool.Success(res);
		}


		/// <summary>
		/// 个人修改密码、管理员修改密码
		/// </summary>
		/// <param name="userName"></param>
		/// <returns></returns>
		[HttpPost("UpdateAuthor")]
		[Authorize]
		public async Task<ActionResult<ApiResult>> UpdateAuthor(string userName,int roleId)
		{
			Author author = await _authorService.FindByIdAsync(Convert.ToInt32(this.User.FindFirst("Id").Value));
			if (author == null) return ApiResultTool.Fail("没有找到用户");
			author.UserName = userName;
			author.RoleId = roleId;

			bool res = await _authorService.EditAsync(author);
			if (!res) return ApiResultTool.Fail("修改失败");
			return ApiResultTool.Success(res);
		}


		[HttpPost("FindAuthorInfor")]
		[Authorize]
		public async Task<ActionResult<ApiResult>> FindAuthorInfor([FromServices]IMapper mapper, int id)
		{
			Author author = await _authorService.FindByIdAsync(id);
			if (author == null) return ApiResultTool.Fail("没有找到用户");

			AuthorDto authorShow =  mapper.Map<Author, AuthorDto>(author);
			return ApiResultTool.Success(authorShow);
		}
	}
}
